/**
 * 
 */
package no.mehdi.game.repository;

import java.util.List;

import no.mehdi.game.domain.Player;

import org.springframework.orm.hibernate3.HibernateTemplate;

/**
 * Player repository implementation
 * 
 * @author Mehdi Zare
 * 
 */
public class PlayerRepositoryImpl implements PlayerRepository {
	
	/** The hibernateTemplater */
	HibernateTemplate hibernateTemplate;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * no.mehdi.game.repository.PlayerRepository#getPlayerById(java.lang.Long)
	 */
	@Override
	public Player getPlayerById(Long playerId) {
		return hibernateTemplate.get(Player.class, playerId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * no.mehdi.game.repository.PlayerRepository#getPlayerByName(java.lang.String
	 * )
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Player getPlayerByName(String playerName) {
		List<Player> playerList = hibernateTemplate.find("from Player where playerName=?", playerName);
		if (playerList.isEmpty()) {
			return null;
		}
		return playerList.get(0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * no.mehdi.game.repository.PlayerRepository#savePlayer(no.mehdi.game.domain
	 * .Player)
	 */
	@Override
	public void savePlayer(Player player) {
		hibernateTemplate.saveOrUpdate(player);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * no.mehdi.game.repository.PlayerRepository#deletePlayer(no.mehdi.game.
	 * domain.Player)
	 */
	@Override
	public void deletePlayer(Player player) {
		hibernateTemplate.delete(player);
	}

	/**
	 * @param hibernateTemplate
	 *            the hibernateTemplate to set
	 */
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

}
